package com.rsscollector.feedrss.parser.parserimpl;

import com.rsscollector.feedrss.parser.GenericParser;
import com.rsscollector.feedrss.FeedReader;
import com.rsscollector.util.StringUtil;
import com.rsscollector.util.IO;
import com.sun.syndication.feed.synd.SyndEntry;

import java.net.URL;
import java.net.MalformedURLException;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

public class ThanhNienParser extends GenericParser {

    private static Logger logger = Logger.getLogger(VnExpressParser.class.getName());
    public static final String END_TEXT = "</table>";

    /**
     * Get image link if have
     *
     * @param content: content of rss feed
     * @param key
     *      web.thanhnien.com.vn=com.rsscollector.feedrss.parser.parserimpl.ThanhNienParser
     * @param matchStr: one element object, it is gotten from content and build to SyndEntry object
     * @return image link
     */
    public synchronized String getImageLink(String content, String key, SyndEntry matchStr) {
        String result = null;
        String temp = null;
        if (matchStr != null) {
            //http://www1.thanhnien.com.vn/Thethao/2007/8/12/204663.tno
            temp = matchStr.getLink();
            int pos = temp.indexOf("thanhnien.com.vn");
            if (pos >= 0) {
                //thanhnien.com.vn/Thethao/2007/8/12/204663.tno
                temp = temp.substring(pos);
                temp = temp.replaceAll("thanhnien.com.vn/", "");
            }
            temp += "\"><span class='first_box_title'>";
        }
        if (content != null) {
            int startIndex = content.indexOf(temp);
            while (startIndex >= 0) {
                int endIndex = content.indexOf(END_TEXT, startIndex);
                String sub;
                if (startIndex < endIndex && startIndex >= 0 & endIndex >= 0) {
                    sub = content.substring(startIndex, endIndex);
                    int startSub = 0;
                    int endSub = 0;
                    if ((startSub = sub.indexOf("<img")) >= 0) {
                        endSub = sub.indexOf(">", startSub + 1);
                        result = sub.substring(startSub, endSub + 1);
                    } else {
                        startIndex = content.indexOf(temp, startIndex + 1);
                    }
                }
                if (result != null) {
//                    int start = result.indexOf("'");
//                    int end = result.indexOf("'", start+1);
//                    String str = null;
//                    if (start >= 0 && end > start && end > 0) {
//                        str = result.substring(start+1, end-1);
//                        result = result.replace(str, "http://web.thanhnien.com.vn"+str);
//                    }
                    break;
                }
            }
        }
        return result;
    }

    public StringBuffer getContent(String url) {
        StringBuffer buf = null;
        try {
            System.out.println("urllllll-="+url);
            String data = IO.getContentFromURL(url);
            if (!StringUtil.isEmpty(data)) {
                int startIndex = data.indexOf("<td style=\"PADDING-RIGHT: 5px\">");
                int endIndex = data.indexOf("</td>", startIndex);
                System.out.println("startIndex="+startIndex);
                System.out.println("endIndex="+endIndex);
                int position = 0;
                if (startIndex >= 0 && endIndex >= 0 && startIndex < endIndex) {
                    data = data.substring(startIndex, endIndex);
                    data = data.substring("<td style=\"PADDING-RIGHT: 5px\">".length(), data.length());
                    data = data.substring(0, data.length() - 5);
                    buf = new StringBuffer(data);
                }
            }
        } catch (Exception e) {
            logger.error("Cannot get content at url:" + url, e);
            /*Don't do anything, because have problem with reading url, we will read this site later*/
        }
        return buf;
    }
}
